/**
 * List Collection is our live storage
 * It holds all the lists in a hashmap
 * This is always synced with the permanent storage
 * Hence access for data is provided from here
 * If data is modified, it is modified here.
 * DataManager then updates the permanent storage with changes made here
 * @author Vaarnan Drolia
 * 
 */
package data;

import java.util.ArrayList;
import java.util.HashMap;

import core.Log;


public class ListCollection
{
	private HashMap<String, EventList> eventListMap;
	private static final int DEFAULT_NUMBER_OF_LISTS = 2;

	public ListCollection() {
		this(DEFAULT_NUMBER_OF_LISTS);
		Log.debug("LC: Entering the default constrcutor");
		Log.warn("LC: Default size is taken to be 2 because size is not specified");
	}

	/**
	 * creates a listcollection of the specified size
	 * @param initSize
	 */
	public ListCollection(int initSize) {
		Log.debug("LC: Entering the constrcutor with size as parameter");
		eventListMap = new HashMap<String, EventList>(initSize);
	}

	/**
	 * adds a list of a specific name to the collection of lists
	 * @param listName
	 */
	public void addList(String listName) {
		Log.debug("LC: Entering addList with listName as param");
		addList(listName, new EventList(listName));
	}

	/**
	 * adds a sepcified event to a particular list
	 * @param listName
	 * @param eventToBeAdded
	 */
	public void addEvent(String listName, Event eventToBeAdded) {
		Log.debug("LC: Entering addEvent with 2 params");
		EventList list = eventListMap.get(listName);
		if (list == null) {
			list = new EventList(listName);
			eventListMap.put(listName, list);
		}
		list.addEvent(eventToBeAdded);
	}

	/**
	 * 
	 * @param nameOfList
	 * @return retrieves the eventlist from the hashmap
	 */
	public EventList get(String nameOfList) {
		Log.debug("LC: Entering get function");
		return eventListMap.get(nameOfList);
	}

	//FIXME what exactly does this function do?!
	/**
	 * 
	 * 
	 * @param listName
	 * @param eventList
	 */
	public void addList(String listName, EventList eventList) {
		Log.debug("LC: Entering addList function with 2 params");
		eventListMap.put(listName, eventList);
	}

	/**
	 * 
	 * @return all the eventlists in the listmap
	 */
	public EventList[] getLists() {
		Log.debug("LC: Entering getLists");
		return eventListMap.values()
				.toArray(new EventList[eventListMap.size()]);
	}

	/**
	 * removes the event from the specified list
	 * @param eventToBeRemoved
	 * @param listName
	 */
	public void removeEventFromList(Event eventToBeRemoved, String listName) {
		Log.debug("LC: Entering removeEventFromList");
		EventList eventList = eventListMap.get(listName);
		eventList.remove(eventToBeRemoved);
	}

	/**
	 * 
	 * @param oldEvent
	 * @param newEvent
	 * @param listName
	 * @return boolean true if the replace was successful, false otherwise
	 */
	public boolean replace(Event oldEvent, Event newEvent, String listName) {
		Log.debug("LC: Entering replace");
		EventList eventList = eventListMap.get(listName);
		if(eventList == null) {
			Log.warn("LC: Could not replace object becasue listName does not exist");
			return false;
		}
		Log.warn("LC: Replaced object in specified list");
		return eventList.replace(oldEvent, newEvent);
	}

	/**
	 * 
	 * @param listName
	 * @return an arraylist of all the events in the specified list
	 */
	public ArrayList<Event> getAllEventsInList(String listName) {
		Log.debug("LC: Entering getAllEventsInList");
		EventList eventList = eventListMap.get(listName);
		if(eventList == null) {
			Log.warn("LC: Could not get events because list does not exist");
			return null;
		}
		ArrayList<Event> listOfEvents = new ArrayList<Event>();
		listOfEvents = eventList.getEvents();
		return listOfEvents;
	}
}
